import 'package:aitao_flutter/common_ui/back_nav.dart';
import 'package:aitao_flutter/style/box_style.dart';
import 'package:aitao_flutter/style/edgeInsets_style.dart';
import 'package:aitao_flutter/style/text_style.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import '../../common_ui/empty_view.dart';
import '../../model/billding_detail_model.dart';
import '../../refresh/custom_refresh_footer.dart';
import '../../refresh/custom_refresh_header.dart';
import '../../request/api.dart';
import '../../request/requestManager.dart';

class WithDrawListPage extends StatefulWidget {
  const WithDrawListPage({super.key});

  @override
  State<WithDrawListPage> createState() => _WithDrawListPageState();
}

class _WithDrawListPageState extends State<WithDrawListPage> {
  final RefreshController _refreshController =
      RefreshController(initialRefresh: false);
  final List<BilldingDetailModel> _list = [];
  int _page = 1;
  bool isLast = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: BackNavigation(
          title: '提现明细',
        ),
        body: _list.isEmpty
            ? EmptyView()
            : SmartRefresher(
                controller: _refreshController,
                enablePullDown: true,
                enablePullUp: isLast == true,
                onRefresh: _onRefresh,
                onLoading: _onLoading,
                header: CustomClassicHeader(),
                footer: const CustomClassicFooter(),
                child: ListView.builder(
                  itemBuilder: ((context, index) {
                    return _bilddingItem(index);
                  }),
                  itemCount: _list.length,
                ),
              ));
  }

  Widget _bilddingItem(int index) {
    BilldingDetailModel item = _list[index];
    return Container(
      padding: EdgeInsetsStyle.edgeInsets(20, 22),
      child: Row(children: [
        Expanded(
            child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              item.itemDesc ?? '',
              style: Styles.normalText_14,
            ),
            Box.vBox5,
            Text(
              item.createTime ?? '',
              style: Styles.greyText_12,
            )
          ],
        )),
        // const Spacer(),
        Box.hBox10,
        Text(
          item.changeDesc ?? '',
          style: Styles.greyText_12,
        )
      ]),
    );
  }

  @override
  void initState() {
    super.initState();
    loadNewList();
  }

  void loadNewList() async {
    var result = await RequestManager()
        .post(API.billingList, params: {'type': 1, 'page': _page});
    if (result != null && result['code'] == 0) {
      _list.clear();
      _page = result['data']['page'];
      int prepage = result['data']['prepage'];
      isLast = prepage < 20;
      List tempList = result['data']['list'];
      for (var element in tempList) {
        BilldingDetailModel m = BilldingDetailModel.fromJson(element);
        _list.add(m);
      }
      if (isLast) {
        _refreshController.loadNoData();
      }
      if (mounted) {
        setState(() {});
      }
    }
  }

  void loadMoreList() async {
    var result = await RequestManager()
        .post(API.followList, params: {'type': 0, 'page': _page});
    if (result != null && result['code'] == 0) {
      _page = result['data']['page'];
      int prepage = result['data']['prepage'];
      isLast = prepage < 20;
      if (isLast) {
        _refreshController.loadNoData();
      }
      List tempList = result['data']['list'];
      _list.addAll(tempList.map((e) {
        BilldingDetailModel m = BilldingDetailModel.fromJson(e);
        return m;
      }));
      if (mounted) {
        setState(() {});
      }
    }
  }

  void _onRefresh() {
    _page = 1;
    isLast = false;
    loadNewList();
    _refreshController.refreshCompleted();
  }

  void _onLoading() async {
    _page += 1;
    loadMoreList();
    _refreshController.loadComplete();
  }
}
